package com.blobfish.fiction.user.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.blobfish.fiction.common.base.exception.GlobalErrorException;
import com.blobfish.fiction.common.model.User;
import com.blobfish.fiction.user.constant.UserMessageEnum;
import com.blobfish.fiction.user.service.IUserService;
import com.jfinal.log.Log;

import java.util.List;

/**
 * 用户service实现类
 * @author huangbo
 */
public class UserServiceImpl implements IUserService {
    private static final Log log = Log.getLog(UserServiceImpl.class);

    private User dao = new User().dao();

    @Override
    public boolean isDuplicateUserName(String userName) {
        List<User> userList = dao.find("select * from user where user_name = ?", userName);
        if (CollectionUtil.isEmpty(userList)) {
            return false;
        } else {
            return true;
        }
    }

    @Override
    public boolean add(User user) {
        return user.save();
    }

    @Override
    public User getByUserName(String userName) {
        User user = dao.findFirst("select * from user where user_name = ?", userName);
        if (user == null) {
            throw new GlobalErrorException(UserMessageEnum.USER_NAME_OR_PASSWORD_WRONG);
        }
        return user;
    }
}
